<?php
/**
 * Created by PhpStorm.
 * User: HM28
 * Date: 2019/4/2
 * Time: 14:42
 */

namespace app\admin\controller;


use think\Db;

class Help extends Base
{
    public $where = [];
    //问题列表
    public function index(){
        $page = input('get.page');
        $limit = input('get.limit');
        if ($page && $limit) {
            $where = $this->where;
            //****搜索功能
            if(input('get.title')){
                $title = input('get.title');
                $where['a.title'] = ['like',"%$title%"];
            }
            if(input('get.cate_id')){
                $cate_id = input('get.cate_id');
                $where['cate1_id|cate2_id|cate3_id'] = $cate_id;
            }
            $start = ($page - 1) * $limit;
            $count = Db::name('help')->count();
            $list = Db::name('help a')
                ->join('admin b','a.admin_id=b.admin_id','LEFT')
                ->where($where)
                ->field('a.id,a.cate1_id,a.cate2_id,a.cate3_id,a.title,a.content,a.update_time,b.user_name')
                ->limit($start, $limit)->select();
            $cate = Db::name('help_cate')->getField('id,name');
            foreach($list as $k=>$v){
                $list[$k]['cate1_id'] = $cate[$v['cate1_id']];
                $list[$k]['cate2_id'] = $cate[$v['cate2_id']];
                $list[$k]['cate3_id'] = $cate[$v['cate3_id']];
            }

            return json(array(
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list
            ));
        }
    }

    //添加和修改问题
    public function add(){
        if(input('post.')){
            if(input('post.id')){
                $data = input('post.');
                $data['update_time'] = date('Y-m-d H:i:s');
                Db::name('help')->update($data);
            }else {
                $data = input('post.');
                unset($data['id']);
                $data['add_time'] = date('Y-m-d H:i:s');
                $data['update_time'] = date('Y-m-d H:i:s');
                $data['admin_id'] = session('admin_id');
                Db::name('help')->insertGetId($data);
            }
            return "<script type='application/javascript'>parent.window.location.reload();</script>";
        }else{
            if(input('get.id')){
                $id = input('get.id');
                $data = Db::name('help')->field('id,cate1_id,cate2_id,cate3_id,title,content')->where('id',$id)->find();
                $this->assign('data',$data);
                //获取第二级分类
                $cate2_res = Db::name('help_cate')->field('id,name')->where('pid',$data['cate1_id'])->select();
                $this->assign('cate2_res',$cate2_res);
                //获取第三级分类
                $cate3_res = Db::name('help_cate')->field('id,name')->where('pid',$data['cate2_id'])->select();
                $this->assign('cate3_res',$cate3_res);
            }
            $cate1 = Db::name('help_cate')->field('id,name')->where('pid',0)->select();
            $cate2 = Db::name('help_cate')->field('id,name')->where('pid',1)->select();
            $this->assign("cate1",$cate1);
            $this->assign("cate2",$cate2);
        }
        return $this->fetch();
    }

    public function cateAjax(){
        $id = input('post.cate_id');
        if($id>0){
            $list = Db::name('help_cate')->where('pid',$id)->select();
            if($list){
                $_return['code'] = 0;
                $_return['result'] = $list;
            }else{
                $_return['code'] = -100;
            }
            return json($_return);
        }
    }

    //删除问题
    public function del(){
        $id = input('post.id');
        if($id>0){
            $res = Db::name('help')->where('id',$id)->delete();
            if($res){
                exit(json_encode(['status'=>1,'msg'=>'删除成功']));
            }else{
                exit(json_encode(['status'=>0,'msg'=>'删除失败']));
            }
        }
    }

    //问题分类列表
    public function indexCate(){
        $help = Db::name('help_cate')->field('id,name,pid as pId,level')->select();
        foreach($help as $k=>$v){
            $help[$k]['open'] = true;//tree树默认展开
        }
        $help_tree = json_encode($help);
        $help_cate = $this->cate_lev($help,0,0);
        $this->assign('help',$help_tree);
        $this->assign('help_cate',$help_cate);
        return $this->fetch();
    }

    public function cate_lev($arr,$id,$level)
    {
        $list =array();
        foreach ($arr as $k=>$v){
            if ($v['pId'] == $id){
                if($level<2){
                    $v['level']=$level;
                    $v['son'] = $this->cate_lev($arr,$v['id'],$level+1);
                }
                $list[] = $v;
            }
        }
        return $list;
    }

    //添加问题分类
    public function addCate(){
        $id = input('post.id');
        if($id>0){
            $data['pid'] = $id;
            $data['name'] = input('post.name');
            $data['level'] = input('post.level')+1;
            $id = Db::name('help_cate')->insertGetId($data);
            exit(json_encode(['id'=>$id]));
        }
    }

    //修改问题分类
    public function editCate(){
        $id = input('post.id');
        if($id>0){
            $data['id'] = $id;
            $data['name'] = input('post.name');
            $res = Db::name('help_cate')->field('id')->where('id',$id)->find();
            if($res){
                Db::name('help_cate')->update($data);
            }
        }
    }

    //删除问题分类
    public function delCate(){
        $id = input('post.id');
        if($id>0){
            $res = Db::name('help_cate')->field('id')->where('id',$id)->find();
            if($res){
                $members = Db::name('help_cate')->field('id,pid')->select();
                $arr = $this->GetTeamMember($members,$id);
                array_push($arr,$id);
                Db::name('help_cate')->where('id','in',$arr)->delete();
            }
        }
    }

    //获取分类的所有下级ID
    function GetTeamMember($members, $mid) {
        $Teams=array();//最终结果
        $mids=array($mid);//第一次执行时候的分类id
        do {
            $othermids=array();
            $state=false;
            foreach ($mids as $valueone) {
                foreach ($members as $key => $valuetwo) {
                    if($valuetwo['pid']==$valueone){
                        $Teams[]=$valuetwo['id'];//找到我的下级立即添加到最终结果中
                        $othermids[]=$valuetwo['id'];//将我的下级id保存起来用来下轮循环他的下级
                        array_splice($members,$key,1);//从所有分类中删除他
                        $state=true;
                    }
                }
            }
            $mids=$othermids;//foreach中找到的我的下级集合,用来下次循环
        } while ($state==true);
        return $Teams;
    }

}